Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

controller open to manage the state. #442

Merged

Conversation

alex-melnyk
Copy link
Contributor

@alex-melnyk alex-melnyk commented Oct 13, 2023

This PR adds the ability to use the controller for the programmatically state management.

@alex-melnyk
Copy link
Contributor Author

@letsar please merge the PR and roll out the new version, this will help me use your plugin from https://pub.dev instead of the git forked version.

@letsar
Copy link
Owner

letsar commented Mar 14, 2024

Hi, thank you for the PR and sorry for the delay. I'll do this today.

@letsar
Copy link
Owner

letsar commented Mar 14, 2024

We can already get the SlidableController from the callback since we have the BuildContext, by using Slidable.of(context) :

SlidableAction(
  flex: 2,
  onPressed: (context) {
    Slidable.of(context)?.openEndActionPane();
  },
  backgroundColor: Color(0xFF7BC043),
  foregroundColor: Colors.white,
  icon: Icons.archive,
  label: 'Archive',
),

Is there other use-cases where it would be useful to pass a controller to the Slidable?

@alex-melnyk
Copy link
Contributor Author

alex-melnyk commented Mar 14, 2024

We can already get the SlidableController from the callback since we have the BuildContext, by using Slidable.of(context) :

SlidableAction(
  flex: 2,
  onPressed: (context) {
    Slidable.of(context)?.openEndActionPane();
  },
  backgroundColor: Color(0xFF7BC043),
  foregroundColor: Colors.white,
  icon: Icons.archive,
  label: 'Archive',
),

Is there other use-cases where it would be useful to pass a controller to the Slidable?

You are right, but this adds complexity to managing this controller from the parent widgets.
BTW I did find out how to get a controller in another way:

final _slidableKey = GlobalKey();
// some code...
return GestureDetector(
          onSecondaryTap: widget.swipeable && !widget.selectable
              ? () => _slidableController.openEndActionPane()
              : null,
          onLongPress: () => _slidableController.openEndActionPane(),
          child: Slidable(
            key: _slidableKey,
            enabled: widget.swipeable && !widget.selectable,
            endActionPane: ActionPane(
              extentRatio: extentRatio,
              motion: const DrawerMotion(),
              children: [
                if (widget.conversation.contactType.isUnknown)
                  ChatListSlidableAction(
                    label: FlutterI18n.translate(context, 'cta.delete'),
                    backgroundColor: theme.colorScheme.error,
                    onPressed: widget.onDeletePressed,
                    icon: QliqIcons.delete,
                  )
                else ...[
                  ChatListSlidableAction(
                    onPressed: widget.onMutePressed,
                    backgroundColor: AppColors.darkGrey,
                    icon: widget.conversation.isMuted
                        ? QliqIcons.muteConversation
                        : QliqIcons.muteConversation,
                    label: widget.conversation.isMuted
                        ? FlutterI18n.translate(context, 'cta.unmute')
                        : FlutterI18n.translate(context, 'cta.mute'),
                  ),
                  ChatListSlidableAction(
                    onPressed: widget.onStarPressed,
                    backgroundColor: theme.primaryColor,
                    icon: widget.conversation.isStarred
                        ? Icons.star
                        : Icons.star_border,
                    label: widget.conversation.isStarred
                        ? FlutterI18n.translate(context, 'cta.unstar')
                        : FlutterI18n.translate(context, 'cta.star'),
                  ),
                ],
                ChatListSlidableAction(
                  onPressed: widget.onArchivePressed,
                  backgroundColor: AppColors.darkGrey,
                  icon: QliqIcons.archiveConversation,
                  label: FlutterI18n.translate(context, 'cta.archive'),
                ),
              ],
            ),
            child: conversationWidget,
          ),
        );
  /// Since the [Slidable] widget state is private, we need access
  /// to its controller for the ability to open the action pane.
  ///
  /// Returns [SlidableController] from [Slidable] widget state.
  SlidableController get _slidableController =>
      (_slidableKey.currentState as dynamic).controller; // SO HERE is the not-so-good way to get controller, but...

So it will be very helpful if the SlidableState is not private then the global key can be typed properly to access the members such as controller.

@letsar
Copy link
Owner

letsar commented Mar 14, 2024

Ok I'll merge this then ;-)

@letsar letsar merged commit e14ff90 into letsar:master Mar 14, 2024
@letsar
Copy link
Owner

letsar commented Mar 14, 2024

Published in 3.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants